/*
 * Quendor - A Z-Machine Interpreter for Android
 * Copyright (c) 2011 Michael Kleinhenz, michael@kleinhenz.net
 *
 * Quendor is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 3 
 * as published by the Free Software Foundation.
 *
 * Quendor is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Quendor.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.kleinhenz.quendor.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.InputType;
import android.text.method.ArrowKeyMovementMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class QView extends LinearLayout {

	public static final Typeface DEFAULT_TYPEFACE = Typeface.SERIF;
	public static final float DEFAULT_TYPESIZE = 16;
	public static final int DEFAULT_TEXTCOLOR = Color.BLACK;
	public static final int DEFAULT_BACKGROUND = Color.LTGRAY;

	public static float TYPESIZE = DEFAULT_TYPESIZE;
	
	private List<View> windows = new ArrayList<View>();
	
	Logger log = Logger.getLogger("Quendor UI");

	public QView(Context context) {
		super(context);
	}

	public QView(Context context, AttributeSet attributes) {
		super(context, attributes);
	}

	public int getTerminalHeight() {
		// TODO calculate
		return 25;
	}

	public int getTerminalWidth() {
		// TODO calculate
		return 80;
	}

	// FIXME: order of windows is reversed imho
	public void createScreen(OnKeyListener listener, int currentWindow, QWindow window) {
		log.info("Creating new TextView with height=" + window.height + " width=" + window.width +". CurrentWindow is " + currentWindow + ". New window is " + window.id + ".");

		// create views, setting defaults
		View resultingView = null;
		
		// setting layout parameters
		if (window.id==0) {
			log.info("Creating window 0, setting weight to max, setting typeface to Roman and adding key listener.");
			EditText tv = new EditText(getContext());
			tv.setTextSize(TYPESIZE);
			tv.setTextColor(DEFAULT_TEXTCOLOR);
			tv.setBackgroundColor(DEFAULT_BACKGROUND);
			ScrollView sv = new ScrollView(getContext());
			tv.setTypeface(DEFAULT_TYPEFACE);
			sv.addView(tv);
			tv.setOnKeyListener(listener);
			addView(sv, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 2));			
			resultingView = sv;
		} else {			
			log.info("Creating additional window " + window.id + ", setting weight to min and Monospace as default typeface.");
			TextView tv = new TextView(getContext());
			tv.setTextSize(TYPESIZE);
			tv.setTextColor(DEFAULT_TEXTCOLOR);
			tv.setBackgroundColor(DEFAULT_BACKGROUND);
			View currentWindowView = windows.get(currentWindow);
			int currentWindowIndex = indexOfChild(currentWindowView);
			tv.setBackgroundColor(Color.CYAN);
			tv.setHeight(getLineHeight() * window.height);
			tv.setTypeface(Typeface.MONOSPACE);
			tv.setHeight(tv.getLineHeight() * window.height * 2);
			addView(tv, currentWindowIndex, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));			
			resultingView = tv;
		}

		// add the TextView to the indexed window list
		if (window.id>=windows.size())
			windows.add(resultingView);
		else
			windows.set(currentWindow, resultingView);
	}

	private int getLineHeight() {
		// TODO Auto-generated method stub
		return 10;
	}

	public void updateWindow(QWindow qWindow, String uuid) {
		String dump = dumpQWindow(qWindow);
		log.info("Updating window " + qWindow.id + " (" + uuid + "): " + shorten(dump) + "...");
		
		View indexedView = windows.get(qWindow.id);

		if (indexedView instanceof TextView)
			((TextView)indexedView).setText(dumpQWindow(qWindow));
		else
			((TextView)((ScrollView)windows.get(qWindow.id)).getChildAt(0)).setText(dumpQWindow(qWindow));
	}

	private String dumpQWindow(QWindow qw) {
		// FIXME: implement styling
		StringBuffer buf = new StringBuffer();
		for (List<QCharacter> l : qw.screen)
			for (QCharacter c : l)
				buf.append((char)c.c);
		return buf.toString();
	}

	private String shorten(String in) {
		if (in.trim().length()<30)
			return in.trim();
		else
			return in.trim().substring(0, 29);
	}
}
